<?php
namespace AdminBundle\Repository;


class BaseDeviceRepository extends BaseRepository
{
    public function pageQuery($_params, $page)
    {
        $sql = <<<EOF
select 
    id, name, note, is_enabled as isEnabled, creator, ip, ctime 
     , imei, group_id as groupId, group_name as groupName
from base_device 
where 1=1 
EOF;
        $params = array();
        if (!empty($_params)) {
/*            if (isset($_params['starttime'])) {
                $sql .= ' and ctime > :starttime ';
                $params['starttime'] = $_params['starttime'];
            }
            if (isset($_params['endtime'])) {
                $sql .= ' and ctime < :endtime ';
                $params['endtime'] = $_params['endtime'];
            }*/
            if (isset($_params['keyword']) && !empty($_params['keyword'])) {
                $sql .= ' and name like :keyword ';
                $params['keyword'] = '%' . $_params['keyword'] . '%';
            }
            if (isset($_params['device_group']) && !empty($_params['device_group'])) {
                $sql .= ' and group_id = :group_id ';
                $params['group_id'] = $_params['device_group'] ;
            }
        }
        $sql .= ' ORDER BY id DESC';

        return $this->pageWithRawSql($sql, $params, $page);
    }

    /**
     * 根据组id,获取已经分配的设备详细信息
     * 用途: 后台定时任务分发任务
     * @param $_params
     * @param int $page
     * @return bool
     * @throws \Exception
     */
    public function deviceByGroup($_params)
    {
        $sql = <<<EOF
SELECT 
    id, name, imei
FROM
    cloud_control.base_device
WHERE
    group_id = :group_id AND is_enabled = 1;
EOF;
        $params = ['group_id' => $_params['group_id']];

        return $this->rawQuery($sql, $params);
    }
}
